﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using ANZ_AIM_DAL;
using System.Globalization;
using ANZ_AIM_BLL.Model;
using ANZ_AIM_BLL.Utils;
using ANZ_AIM_FRAMEWORK;

namespace ANZ_AIM_BLL
{
    public class SaleChangeTempBLL
    {
        private string ConnectionString;
        private const string TABLE_NAME = "SaleChangeTemp";
        private Dictionary<string, string> ObjectPropDict;

        public SaleChangeTempBLL()
        {
            ConnectionString = DBHelper.GetMainMdbConnectionString();
            this.ObjectPropDict = BuildObjectPropertyMapping();
        }

        public void Save(List<SaleChangeTemp> SaleChangeTempList)
        {
            try
            {
                var dict = MiscHelper.Clone(this.ObjectPropDict);
                OleDbParameter[] parameters;
                string command = string.Empty;
                foreach (SaleChangeTemp saleChange in SaleChangeTempList)
                {
                    command = CommandHelper.BuildInsertCommand(saleChange, dict, out parameters);
                    OleDbHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, command, parameters);
                }
            }
            catch (Exception ex) { throw ex; }
        }

        public void ClearAll() { 
             try
             {
                 OleDbParameter[] parameters;
                 string command = string.Format("DELETE * FROM {0}", TABLE_NAME);
                 var n = OleDbHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, command);
             }
             catch (Exception ex) { throw ex; }
        }

        public DataTable GetSaleChangeTempInfo()
        {
            try
            {
                var dict = MiscHelper.Clone(this.ObjectPropDict);
                string command = string.Empty;
                command = CommandHelper.BuildSelectAllCommand(TABLE_NAME, dict);
                DataSet dsResult = OleDbHelper.ExecuteDataset(this.ConnectionString, CommandType.Text, command);
                return dsResult.Tables[0];
            }
            catch (Exception ex) { throw; }
        }

        //public DataTable GetByBatchFileChangeID(string batchFileChangeID)
        //{
        //    try
        //    {
        //        var dict = MiscHelper.Clone(this.ObjectPropDict);
        //        SaleChangeTemp SaleChangeTemp = new SaleChangeTemp() { BatchFileChangeID = batchFileChangeID };
        //        OleDbParameter[] parameters;
        //        var conditionDict = new Dictionary<string, string>
        //            {
        //                {"BatchFileChangeID", "BatchFileChangeID"}
        //            };
        //        string command = CommandHelper.BuildSelectCommand(SaleChangeTemp, dict, conditionDict, out parameters);
        //        DataSet dsResult = OleDbHelper.ExecuteDataset(this.ConnectionString, CommandType.Text, command, parameters);

        //        return dsResult.Tables[0];
        //    }
        //    catch (Exception ex) { throw ex; }
        //}


        //public void Delete(string batchFileChangeID)
        //{
        //    try
        //    {
        //        var dict = new Dictionary<string, string>
        //            {
        //                {"BatchFileChangeID", "BatchFileChangeID"}
        //            };
        //        SaleChangeTemp SaleChangeTemp = new SaleChangeTemp() { BatchFileChangeID = batchFileChangeID };
        //        OleDbParameter[] parameters;
        //        string command = string.Empty;
        //        command = CommandHelper.BuildDeleteCommand(SaleChangeTemp, dict, out parameters);
        //        int n = OleDbHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, command, parameters);
        //    }
        //    catch (Exception ex) { throw ex; }
        //}

        public Dictionary<string, string> BuildObjectPropertyMapping()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>
            {
                {"ProfileID", "ProfileID"},
                {"BatchFile", "BatchFile"},
                {"FieldChange", "FieldChange"},
                {"OldValue", "OldValue"},
                {"NewValue", "NewValue"},
            };

            return dictionary;
        }

    }
}
